<?php
namespace app\api\controller;

use app\common\controller\Base;
use app\common\exception\FailException;
use app\common\logic\Card as CardLogic;
use think\Loader;
use think\Request;

/**
 * @title 银行卡模块接口
 * @description 接口说明
 * @group 接口分组
 * @header name:token require:1 default: desc:token
 * @param name:user_type type:int require:1 default:1 other: desc:状态(1.会员登陆，2.商家，3.代理商，4.省级代理商，5.内部员工)
 */
class Card extends Base
{
    /**
     * @title 银行卡列表
     * @description 接口说明
     * @author WCJ
     * @url /api/card/card_list
     * @method POST
     * @param name:default type:int require:1 default:1 other: desc:1.选中
     * @param name:page type:int require:1 default:1 other: desc:页数
     * @param name:num type:int require:1 default:1 other: desc:每页数量
     *
     *
     * @return data:数据
     */
       public function card_list(Request $request){
           $userid=$this->uid;
           $usertype=$this->type;
           $default =$request->post("default");
           $page=$request->post("page")?$request->post("page"):1;
           $num=$request->post("num")?$request->post("num"):20;
           $cardLogic=new CardLogic();

           $data = $cardLogic->card_list($usertype,$userid,$default,$page,$num);

           return ["code"=>200,"msg"=>"成功","data"=>$data];

       }

    /**
     * @title 删除银行卡号
     * @description 接口说明
     * @author WCJ
     * @url /api/card/del_card
     * @method POST
     * @param name:card_id type:int require:1 default:1 other: desc:1.卡号ID
     * @param name:pay_password type:int require:1 default:1 other: desc:支付密码
     *
     *
     * @return data:数据
     */
    public function del_card(Request $request){

        $userid=$this->uid;
        $usertype=$this->type;
        $card_id =$request->post("card_id");
        $pay_password=$request->post("pay_password");

        if(!$pay_password){
            throw new FailException(["code"=>201,"msg"=>"请填写支付密码"]);
        }

        $cardLogic=new CardLogic();

        $data = $cardLogic->del_card($userid,$pay_password,$card_id,$usertype);

        return ["code"=>200,"msg"=>"删除成功"];

    }


    /**
     * @title 添加银行卡号
     * @description 接口说明
     * @author WCJ
     * @url /api/card/add_card
     * @method POST
     * @param name:card type:int require:1 default:1 other: desc:1.卡号
     * @param name:pay_password type:int require:1 default:1 other: desc:支付密码
     * @param name:bankname type:int require:1 default:1 other: desc:开户行
     * @param name:name type:int require:1 default:1 other: desc:姓名
     *
     *
     * @return data:数据
     */
    public function add_card(Request $request){

        $uid=$this->uid;
        $usertype=$this->type;
        $card =$request->post("card");
        $bankname =$request->post("bankname");
        $name =$request->post("name");


        $pay_password=$request->post("pay_password");

        if(!$pay_password){
            throw new FailException(["code"=>201,"msg"=>"请填写支付密码"]);
        }
        $validate = Loader::validate("User");

        if (!$validate->scene("card")->check(['card' => $card,'bankname'=>$bankname,'name'=>$name])) {

            $ret=['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()];
            throw new ParameterException($ret);

        }
        $cardLogic=new CardLogic();

        $data = $cardLogic->add_card($uid,$pay_password,$card,$bankname,$name,$usertype);

        return ["code"=>200,"msg"=>"成功","data"=>$data];

    }



}